var express = require('express');
var router = express.Router();
var sqlobj = require('../db/sql_utils');
var moment = require('moment');

router.post('/content/add', function (req, res) {
    console.log("user info:",req.data)
    let time = moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
    const params = [req.body.title, 0, req.body.content, req.data.user_id,time,time];
    sqlobj.conn_query(req, res, sqlobj.sqls.addContent, params)
});

router.post('/content/delete', function (req, res) {
    console.log("content delete id： ",req.body.id)
    let user_id = req.data.user_id;
    const params = [user_id,req.body.id];
    return sqlobj.conn_query(req, res, sqlobj.sqls.deleteContent, params)
});

router.post('/content/update', function (req, res) {
    console.log("update content params:",res,req.data)
    const params = [req.body.title, req.body.content, moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),req.data.user_id,req.body.id];
    return sqlobj.conn_query(req, res, sqlobj.sqls.updateContent, params)
});

router.post('/content/query', function (req, res) {
    console.log("query content params:",req.body)
    const sub_params = [];
    sub_params[0] = req.body.page;
    sub_params[1] = req.body.size;
    const params = [req.data.user_id,(parseInt(sub_params[0]) - 1) * parseInt(sub_params[1]), parseInt(sub_params[1])]
    return sqlobj.get_data_conn(req, res, sqlobj.sqls.queryContent, params, sqlobj.sqls.contentConut, sub_params)
});

module.exports = router
